package fastwave.cloud.demo.fastwave.biz.subscriber.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>Title: EmailConfig</p>
 * <p>Description: </p>
 *
 * @author: dwLiu
 * @date: 2020/4/6 23:41
 */
@Configuration
public class EmailConfig {

    /**
     * 交换机
     * @return
     */
    @Bean
    DirectExchange EmailExchange(){
        return new DirectExchange("EmailExchange");
    }

    /**
     * 队列(修改队列后，重启报错，需要删除rabbit里面已经创建的队列)
     * o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error;
     * protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg
     * 'x-dead-letter-exchange' for queue 'EmailQueue' in vhost '/': received the value 'EmailDlExchange' of type
     * 'longstr' but current is none, class-id=50, method-id=10)
     * @return
     */
    @Bean
    Queue EmailQueue(){
//        return new Queue("EmailQueue");
        return QueueBuilder.durable("EmailQueue").withArgument("x-dead-letter-exchange","EmailDlExchange")
                .withArgument("x-dead-letter-routing-key","EmailDlRouting")
                .build();
    }

    /**
     * 交换机和队列绑定
     * @return
     */
    @Bean
    Binding BindEmail(){
        return BindingBuilder.bind(EmailQueue()).to(EmailExchange()).with("EmailRouting");
    }


    /**
     * 死信队列交换机
     * @return
     */
    @Bean
    DirectExchange EmailDlExchange(){
        return new DirectExchange("EmailDlExchange");
    }

    /**
     * 死信队列队列
     * @return
     */
    @Bean
    Queue EmailDlQueue(){
        return new Queue("EmailDlQueue",true);
    }

    /**
     * 死信队列交换机和队列绑定
     * @return
     */
    @Bean
    Binding BindDlEmail(){
        return BindingBuilder.bind(EmailDlQueue()).to(EmailDlExchange()).with("EmailDlRouting");
    }
}
